import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

import javax.sound.midi.Receiver;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ServerFrame extends JFrame {
	
	private JTextField portField;
	private JDialog startDlg;
	private JList gameLst;
	
	private Vector<Game> games;
	
	private ServerFrame() {
		super("XO - Server");
		
		final JButton btnStart = new JButton("Start");
		add(btnStart, BorderLayout.NORTH);
		
		games = new Vector<Game>();
		
		gameLst = new JList(games);
/*		gameLst.setModel(new DefaultListModel() {
			
			@Override
			public Object getElementAt(int arg0) {
				synchronized (games) {
					Game g = games.get(arg0);
					return "<html>Game#" + g.hashCode() + ": " + g.info + " - <img src='file:x.png' alt='X: '></img> " + g.x +
							" - <img src='file:y.png' alt='Y: '></img> " + g.y + " - <img src='file:k.png' alt='K: '></img> " + g.k +
							(g.start == 0 ? " - <img src='file:owner.png' alt='Game owner'></img> " : (g.start == 1 ? 
							" - <img src='file:opponent.png' alt='Opponent'></img> " : " - <img src='file:random.png' alt='Random'></img> ")) + "</html>";
				}
			}

			@Override
			public int getSize() {
				synchronized (games) {
					return games.size();
				}
			}

		});*/

		add(gameLst, BorderLayout.CENTER);

		startDlg = new JDialog();
		startDlg.setLayout(new GridLayout(2, 2));
		
		JTextArea staticText = new JTextArea("Port:");
		staticText.setEditable(false);
		startDlg.add(staticText);
		portField = new JTextField("8128");
		startDlg.add(portField);
		JButton btnOk = new JButton("OK");
		JButton btnCancel = new JButton("Cancel");
		startDlg.add(btnOk);
		startDlg.add(btnCancel);
		
		btnOk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					int port = Integer.parseInt(portField.getText());
					startDlg.setVisible(false);
					
					ServerEngine.getInstance().init(port);
					ServerEngine.getInstance().start();
					
					btnStart.setEnabled(false);
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(startDlg, "Incorrect port");
				}
			}
		});
		
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				startDlg.setVisible(false);				
			}
		});
		
		btnStart.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				startDlg.setVisible(true);
			}
		});
		
		gameLst.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2) {
					int index = gameLst.locationToIndex(e.getPoint());
					if (index != -1) {
						Rectangle rect = gameLst.getCellBounds(index, index);
						if (rect.contains(e.getPoint()))
							games.get(index).createDlg();
					}
				}
			}
		});
		

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				ServerEngine.getInstance().disconnectAll();
				System.exit(0);
			}
		});
	}
	
	public void start() {
		setSize(400, 700);
		setVisible(true);
		startDlg.setSize(200, 70);
		startDlg.setVisible(true);
	}
	
	public void addGame(Game g) {
		synchronized (games) {
			games.add(g);
		}
		gameLst.setListData(games);
	}
	
	public void removeGame(Game g) {
		synchronized (games) {
			games.remove(g);
		}
		gameLst.setListData(games);
	}
	
	private static class SingletonHolder {
		public static ServerFrame instance = new ServerFrame();
	}
		 
	public static ServerFrame getInstance() {
		return SingletonHolder.instance;
	}
}
